package me.dqn.noway.precenter;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import me.dqn.noway.InformationManagerCallback;
import me.dqn.noway.R;
import me.dqn.noway.activity.MainActivity;
import me.dqn.noway.bean.NoWayContext;
import me.dqn.noway.bean.PasswordInformation;
import me.dqn.noway.bean.PasswordUser;
import me.dqn.noway.model.InformationManager;

import static android.content.ContentValues.TAG;

public class MyPresenter implements InformationManagerCallback {

    public static final int GET_DATA_FAILED = 0;
    public static final int UPDATE_VIEW = 1;
    public static final int ADD_INFO_FAILED = 2;
    public static final int POP_MESSAGE = 3;

    private MainActivity activity;
    private InformationManager informationManager;
    private List<PasswordInformation> passwordInformations;

    public MyPresenter() {
        passwordInformations = new ArrayList<>();
    }

    /**
     * 顺便初始化context
     *
     * @param activity
     */
    public void setActivity(MainActivity activity) {
        this.activity = activity;
        informationManager = new InformationManager(activity);
    }


    /**
     * 获取所有的信息
     */
    public void getAllInfos() {
        PasswordUser passwordUser = NoWayContext.getINSTANCE().getPasswordUser();
        if (passwordUser == null || !passwordUser.validate()) {
            activity.popLoginDialog();
        } else {
            informationManager.getAllPasswordInformation(NoWayContext.getINSTANCE().getPasswordUser(), this);
        }
    }

    /**
     * 添加一个新的信息
     *
     * @param passwordInformation
     */
    public void addInfo(PasswordInformation passwordInformation) {
        PasswordUser passwordUser = NoWayContext.getINSTANCE().getPasswordUser();
        if (passwordUser == null || !passwordUser.validate()) {
            activity.popLoginDialog();
        } else {
            informationManager.addPasswordInformation(passwordUser, passwordInformation, this);
        }
    }

    /**
     * 退出程序
     */
    public void quit() {
        this.passwordInformations.clear();
        this.activity.mainRecycleView.getAdapter().notifyDataSetChanged();
        NoWayContext.getINSTANCE().setPasswordUser(null);
    }

    /**
     * 弹出toast
     *
     * @param msg
     * @param time
     */
    public void toastMessages(String msg, int time) {
        Message message = new Message();
        message.what = POP_MESSAGE;
        Bundle bundle = new Bundle();
        bundle.putString("msg", msg);
        bundle.putInt("time", time);
        message.setData(bundle);
        this.activity.handler.sendMessage(message);
    }

    /**
     * 删除相应id的信息
     *
     * @param id
     */
    public void deleteInformation(int id) {
        informationManager.deletePasswordInformationById(NoWayContext.getINSTANCE().getPasswordUser(), id, this);
    }

    /**
     * invorked after get all information done
     *
     * @param passwordInformations
     */
    @Override
    public void onGetAllInformationDone(List<PasswordInformation> passwordInformations) {
        Message message = new Message();
        message.what = MyPresenter.UPDATE_VIEW;
        message.obj = passwordInformations;
        this.activity.handler.sendMessage(message);
    }

    /**
     * 获取信息失败
     *
     * @param failedReason
     */
    @Override
    public void onGetInformationFailed(String failedReason) {
        Message message = new Message();
        message.what = MyPresenter.GET_DATA_FAILED;
        message.obj = "failed to get data.(" + failedReason + ")";
        this.activity.handler.sendMessage(message);
    }


    /**
     * 添加信息成功
     */
    @Override
    public void onAddInformationDone() {
        Log.i(TAG, "onAddInformationDone: done.");
        getAllInfos();
    }

    /**
     * 添加信息失败
     *
     * @param reason
     */
    @Override
    public void onAddInformationFailed(String reason) {
        Log.i(TAG, "onAddInformationFailed: " + reason);
        Message message = new Message();
        message.what = ADD_INFO_FAILED;
        message.obj = "failed to add new information.";
        this.activity.handler.sendMessage(message);
    }

    @Override
    public void onDeleteInformationDone() {
        toastMessages(activity.getString(R.string.DELETE_SUCCESS), Toast.LENGTH_SHORT);
        getAllInfos();
    }

    @Override
    public void onDeleteInformationFailed(String reason) {
        toastMessages(reason, Toast.LENGTH_SHORT);
    }
}
